package com.fusion.ui.windows;

import com.fusion.core.comm.rpc.ChatMessageV3;
import com.fusion.core.node.Friend;
import com.fusion.ui.UISubsystem;

import java.io.IOException;

/**
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 12, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public class PublicChatMessageMDIWindow extends AbstractChatMessageMDIWindow {

	private static final long serialVersionUID = 1L;

	public PublicChatMessageMDIWindow(UISubsystem ui) throws Exception {
        super(ui.getMainWindow().getMDIManager(), "publicchat", ui);

        setTitle("Chat");

        postInit();
    }

    public void send(final String text) throws Exception {
        if (text == null || text.trim().length() == 0) return;
        ui.getCore().invokeLater(new Runnable() {
            public void run() {
                try {
                    for(Friend f : ui.getCore().getFriendManager().friends()) {
                        ui.getCore().getFriendManager().getNetMan().sendPersistantly(new ChatMessageV3(text, true), f);
                    }
                } catch(IOException e) {
                    ui.getCore().reportError(e, this);
                }
            }
        });
        chat.setText("");
        ui.getMainWindow().publicChatMessage(ui.getCore().getFriendManager().getMe().getGuid(), text, System.currentTimeMillis(), false);
    }

    public String getIdentifier() {
        return "publicchat";
    }
}
